<?php
Zend_Loader::loadClass('Lead_Sql_Query');

class Led_Entity_Collection implements Iterator, Countable, ArrayAccess
{
    
    private $_appid;

    private $_typeid;
    
    private $_langid;
    
    private $_listid;
    
    private $_parentid;
    
    private $_baseid;
    
    
    private $_minOrder;
    
    private $_maxOrder;
    
    private $_specialOrder;
    
    private $_getGlobals = false;
    
    private $_getUnpublished = false;
    
    private $_isFetched = false;
    private $_res = array();
    
    private $_currIndex = 0;
        
        
    public function __construct($appid = null, $typeid = null, $langid = null, $listid = null, $parentid = null, $baseid = null)
    {
        $this->_appid = $appid;
        $this->_typeid = $typeid;
        $this->_langid = $langid;
        $this->_listid = $listid;
        $this->_parentid = $parentid;
        $this->_baseid = $baseid;
    }
    
    
    public function getIterator()
    {
        $iter = new ArrayObject($this->_res);
        return $iter;
    }
    
    
    public function getSQL()
    {
        $query = new Lead_Sql_Query(false);

        $query->addField('led_tree.id', true);
        $query->addField('edo.type_id', false);
        $query->addJoin('edo', 'INNER', 'led_tree.id = edo.id', false);
        
        if(!$this->_getGlobals) {
            
            if(is_numeric($this->_listid))
                $query->getCurrentWhereGroup()->addWhereCond('led_tree.list_id', '=', $this->_listid);

            if(is_numeric($this->_parentid))
                $query->getCurrentWhereGroup()->addWhereCond('led_tree.parent_id', '=', $this->_parentid);

        } else {

            if(!is_numeric($this->_parentid))
                throw new Exception('Cant get globals when theres no parent');
            
            $query->getCurrentWhereGroup()->setWhereGroupOperator('OR');
            $query->getCurrentWhereGroup()->addWhereGroup();
                       
                        
            $route = Led_Application::getInstance()->getRoute();
            array_unshift($route, 0);
            
            
            $query->getCurrentWhereGroup()->getCurrentWhereGroup()->
            addWhereCond('led_tree.list_id', '=', $this->_listid);
            
            $query->getCurrentWhereGroup()->getCurrentWhereGroup()->
            addWhereCond('led_tree.parent_id', '=', $this->_parentid);
            
            $query->getCurrentWhereGroup()->addWhereGroup();
            
            $query->getCurrentWhereGroup()->getCurrentWhereGroup()->
            addWhereCond('led_tree.list_id', '=', 2000000002);

            $query->getCurrentWhereGroup()->getCurrentWhereGroup()->
            addWhereCond('led_tree.parent_id', 'in', $route);
            
            $query->addWhereGroup();
            
        }
           
        if(is_numeric($this->_appid))
            $query->getCurrentWhereGroup()->addWhereCond('led_tree.app_id', '=', $this->_appid);

        if(is_numeric($this->_typeid))
            $query->getCurrentWhereGroup()->addWhereCond('edo.type_id', '=', $this->_typeid);
                
        if(is_numeric($this->_langid))
            $query->getCurrentWhereGroup()->addWhereCond('led_tree.lang_id', '=', $this->_langid);

        if(is_numeric($this->_baseid))
            $query->getCurrentWhereGroup()->addWhereCond('led_tree.base_id', '=', $this->_baseid);
            

        if(is_numeric($this->_minOrder))
            $query->getCurrentWhereGroup()->addWhereCond('led_tree.order_id', '>=', $this->_minOrder);

        if(is_numeric($this->_maxOrder))
            $query->getCurrentWhereGroup()->addWhereCond('led_tree.order_id', '<=', $this->_maxOrder);

            
        if(!$this->_getUnpublished)
            $query->getCurrentWhereGroup()->addWhereCond('led_tree.published', '=', 1);

            
        $query->addOrderer('order_id, type_id, lang_id, list_id, base_id');
        
          
        return $query->getSQL();
                        
    }
    
    
    public function render($mode = 'retail')
    {
        ob_start();
        
        if(!$this->isFetched()) 
            $this->fetch();
        
         
        
        /*
        print "<hr />";
        // print $this->getSQL();
        
        // print_r($this->_res);
        
        print "<hr />";
        */
                      
        $renderer = Led_Entity_Renderer::getInstance();

        
        
        
        foreach($this as $entity) {
           
            // $entity = $app->getEdo()->fetch($row['id'], $row['type_id']);
           print $renderer->render($entity, $mode);
        }
        
        return ob_get_clean();
    }
    
    
    public function fetch()
    {
        $this->_res = array();
        
        $app = Led_Application::getInstance();
        $res = $app->getDb()->fetchAll($this->getSQL());

        foreach($res as $row)
            $this->_res[] = $row;
        
        $this->_isFetched = true;
    }
    
    
    public function isFetched()
    {
        return $this->_isFetched;
    }
    
    
    
    public function getGlobals($getGlobals)
    {
        $this->_getGlobals = $getGlobals;
    }
    
    public function getUnpublished($getUnpublished)
    {
        $this->_getUnpublished = $getUnpublished;
    }
    
    public function setMinOrder($minOrder)
    {
        $this->_minOrder = $minOrder;
    }

    public function getMinOrder()
    {
        return $this->_minOrder;
    }
    
    
    public function setMaxOrder($maxOrder)
    {
        $this->_maxOrder = $maxOrder;
    }

    public function getMaxOrder()
    {
        return $this->_maxOrder;
    }

    
    public function count()
    {
        if(!$this->isFetched())
            $this->fetch();
            
        return sizeof($this->_res);
    }
    
    
    public function removeItem($key)
    {
        if(key_exists($key, $this->_res))
            unset($this->_res[$key]);
    }
        
    
    
    
    public function current()
    {
    	if(!$this->_isFetched)
		  $this->fetch();

        
        if($this->valid())
		{
		    return $this->getItem($this->_currIndex);
		}
		return false;
	}

	
	public function valid()
	{
		if(!$this->_isFetched)
		  $this->fetch();
	    
	    return ($this->_currIndex < $this->count());
	}

	
	public function next() {
		$this->_currIndex++;
	}

	
	public function key()
	{
		return $this->_currIndex;
	}

	
	
	public function rewind()
	{
		$this->_currIndex = 0;
	}
	
	
	public function getItem($itemid)
	{
	    
	    $obj = Led_Application::getInstance()->getEdo()->fetch($this->_res[$itemid]['id'], $this->_res[$itemid]['type_id']);
		return $obj;
        
	}

	
	public function offsetGet($offset)
	{
	    return $this->getItem($offset);
	}
	
	
	public function offsetExists($offset)
	{
        if(!$this->_isFetched)
		  $this->fetch();
	    
	    return (key_exists($offset, $this->_res));

	}
	
	
	public function offsetUnset($offset)
	{
	    return false;
	}
	
	
	public function offsetSet($offset, $value)
	{
        return false;
	}
	
	
	public function slice($offset, $length)
	{
	    if(!$this->_isFetched)
	       $this->fetch();
	    
	    $this->_res = array_slice($this->_res, $offset, $length);
	    
	}

    
}
